"use strict";
const common_vendor = require("../../common/vendor.js");
const common_request = require("../../common/request.js");
const _sfc_main = {
  onShareAppMessage() {
    return {
      title: "城市水质查询",
      imageUrl: ""
      // path: ""
    };
  },
  data() {
    return {
      goodsId: "",
      provinceData: [],
      source: "",
      items: [
        {
          "tds_mean": "-",
          "tds_unit": "-",
          "tds_limit": ""
        },
        {
          "hardness_mean": "-",
          "hardness_unit": "-",
          "hardness_limit": "-"
        },
        {
          "turbidity_mean": "-",
          "turbidity_unit": "-",
          "turbidity_limit": "-"
        },
        {
          "chlorine_residual": ""
        }
      ],
      currentProvince: -1,
      currentCity: -1,
      currentOps: [],
      currentSelect: -1,
      currentType: -1
    };
  },
  onLoad(options) {
    this.getWaterQuality();
  },
  methods: {
    onPopupChange(e) {
      const { show } = e;
      if (!show) {
        this.currentType = -1;
      }
    },
    updateQuery() {
      const province = this.currentProvince;
      const city = this.currentCity;
      common_vendor.index.showLoading({ icon: "none" });
      const provinceItem = this.provinceData.find((e) => e.province === province);
      let cityItem = null;
      if (provinceItem) {
        cityItem = provinceItem.city.find((e) => e.city === city);
        if (cityItem) {
          setTimeout(() => {
            this.source = cityItem.list[cityItem.list.length - 1].source;
            this.items = cityItem.list;
            console.log(cityItem);
            common_vendor.index.hideLoading();
          }, 500);
        }
      }
      if (!cityItem) {
        common_vendor.index.showToast({ title: "暂无数据", icon: "none" });
      }
    },
    setSelect(item) {
      if (this.currentType === "province") {
        this.currentProvince = item;
        const citys = ((this.provinceData.find((e) => e.province === this.currentProvince) || {}).city || []).map((e) => e.city);
        this.currentCity = citys && citys[0] || -1;
        this.updateQuery();
      } else {
        this.currentCity = item;
        this.updateQuery();
      }
      this.currentSelect = item;
      this.$refs.popup.close();
      this.currentType = -1;
    },
    showPopup(type) {
      this.currentType = type;
      if (type === "province") {
        this.currentOps = this.provinceData.map((e) => e.province);
      } else {
        this.currentOps = ((this.provinceData.find((e) => e.province === this.currentProvince) || {}).city || []).map((e) => e.city);
      }
      this.$refs.popup.open("bottom");
    },
    getWaterQuality() {
      common_request.service({
        url: "/water/quality"
      }).then((res) => {
        console.log(res);
        if (!res)
          return;
        const keys = {};
        res.forEach((item) => {
          const { province, city, chlorine_residual, list } = item;
          if (!keys[province]) {
            keys[province] = [];
          }
          keys[province].push({
            city,
            chlorine_residual,
            list
          });
        });
        const provinces = [];
        const firstData = keys["北京"];
        if (firstData) {
          console.log(firstData);
          provinces.push({ province: "北京", city: firstData });
          this.currentProvince = "北京";
          this.currentCity = "北京";
          this.currentOps = (firstData || []).map((e) => e.city);
          delete keys["北京"];
        }
        Object.keys(keys).forEach((key) => {
          provinces.push({ province: key, city: keys[key] });
        });
        this.provinceData = provinces;
        this.updateQuery();
        console.log("provinces: ", provinces);
      });
    },
    goToSoftPage() {
      common_vendor.index.navigateTo({
        url: `/pages/softwater/softwater`
      });
    }
  }
};
if (!Array) {
  const _easycom_uni_popup2 = common_vendor.resolveComponent("uni-popup");
  _easycom_uni_popup2();
}
const _easycom_uni_popup = () => "../../uni_modules/uni-popup/components/uni-popup/uni-popup.js";
if (!Math) {
  _easycom_uni_popup();
}
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return common_vendor.e({
    a: common_vendor.t($data.currentProvince === -1 ? "北京" : $data.currentProvince),
    b: $data.currentType === "province" ? 1 : "",
    c: common_vendor.o(($event) => $options.showPopup("province")),
    d: common_vendor.t($data.currentCity === -1 && $data.currentProvince === -1 && "北京市" || $data.currentCity === -1 && $data.currentProvince !== -1 && $data.currentProvince || $data.currentCity !== -1 && $data.currentProvince !== -1 && $data.currentCity + "市"),
    e: $data.currentType === "city" ? 1 : "",
    f: common_vendor.o(($event) => $options.showPopup("city")),
    g: common_vendor.f($data.currentOps, (item, k0, i0) => {
      return {
        a: common_vendor.t(item),
        b: item === $data.currentSelect ? 1 : "",
        c: common_vendor.o(($event) => $options.setSelect(item))
      };
    }),
    h: common_vendor.sr("popup", "0f3416c6-0"),
    i: common_vendor.o($options.onPopupChange),
    j: common_vendor.p({
      type: "bottom",
      ["background-color"]: "#fff"
    }),
    k: common_vendor.t($data.currentCity === -1 ? "北京" : $data.currentCity),
    l: common_vendor.o((...args) => $options.goToSoftPage && $options.goToSoftPage(...args)),
    m: common_vendor.t($data.items[0].tds_mean),
    n: common_vendor.t($data.items[0].tds_unit),
    o: common_vendor.t($data.items[0].tds_limit),
    p: common_vendor.t($data.items[1].hardness_mean),
    q: common_vendor.t($data.items[1].hardness_unit),
    r: common_vendor.t($data.items[1].hardness_limit),
    s: common_vendor.t($data.items[2].turbidity_mean),
    t: common_vendor.t($data.items[2].turbidity_unit),
    v: common_vendor.t($data.items[2].turbidity_limit),
    w: common_vendor.t($data.items[3].chlorine_residual || "根据门店试剂检测"),
    x: $data.source
  }, $data.source ? {
    y: common_vendor.t($data.source || "-")
  } : {});
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__file", "C:/Users/karo/Desktop/finale/3.0/clean-water/clean-water/pages/quality-query/quality-query.vue"]]);
_sfc_main.__runtimeHooks = 2;
wx.createPage(MiniProgramPage);
